import axios from 'axios'
import store from '@/store'
import router from '@/router'
import {Message} from 'element-ui'
const ajax = axios.create({
    baseURL:'http://big-event-vue-api-t.itheima.net',
    timeout:8000,
    headers:{
        "Content-Type":"application/json;charset=utf-8",

    }
})
// 请求拦截
// **********
// ***************************重大发现404/401
// 后台
ajax.interceptors.request.use(config=>{
    // config.headers.Authorization=config.headers.Authorization || {}
    // if(localStorage.getItem('token'))
    console.log(config)
   
   // if(token){ config.headers.token=token}
    const token= config.headers.Authorization=localStorage .getItem('token');
    //  if(token){ config.headers.token=token}
    return config
})
// response相应拦截
ajax.interceptors.response.use(res=>{
    return res
},err=>{
    // 服务器响应发生错误时的处理
    if(err.response.status === 401){
         store.commit('setToken','')
         store.commit('getuserinfo',{})
         router.push('/login')
         Message.error('用户身份已过期')
    } else if(err.response.status === 404){
        router.push('/404')
    }
   return  Promise.reject(err)
})
ajax.interceptors.response.use((config)=>{
    // NProgress.done()
    return config
  })
export default ajax